package com.bestillet.kyllingogco;

import com.google.android.gms.gcm.GoogleCloudMessaging;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

public class GcmIntentService extends IntentService {
	private static final String DEFAULT_MESSAGE = "Lyst til lidt kylling? Bestil dem her.";
	private static final String DEFAULT_HEADER = "Kylling & Co";
	private static final String DEFAULT_BAR = "";
	private static final int DEFAULT_ICON = R.drawable.ic_launcher;

	public static final int NOTIFICATION_ID = 1;
	
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;

	public GcmIntentService() {
		super("GcmIntentService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        String messageType = gcm.getMessageType(intent);
        
        if (!extras.isEmpty()) {

            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                //TODO
            	//sendNotification(DEFAULT_HEADER, "Send error: " + extras.toString(), DEFAULT_URL, DEFAULT_ICON);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                //TODO
            	//sendNotification(DEFAULT_HEADER, "Deleted messages on server: " + extras.toString(), DEFAULT_URL, DEFAULT_ICON);
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
            	// Get notification header
            	String header = extras.getString("header");
            	if (header == null)
            		header = DEFAULT_HEADER;
            	else if (header.equals(""))
            		header = DEFAULT_HEADER;
            	
            	// Get notification bar
            	String bar = extras.getString("bar");
            	if (bar == null)
            		bar = DEFAULT_BAR;
            	else if (bar.equals(""))
            		bar = DEFAULT_BAR;
            	
            	// Get notification message
            	String message = extras.getString("message");
            	if (message == null)
            		message = DEFAULT_MESSAGE;
            	else if (message.equals(""))
            		message = DEFAULT_MESSAGE;
            	
            	// Get notification icon
            	int id = DEFAULT_ICON;
            	String icon = extras.getString("icon");
            	if (icon != null) {
            		try {
        				id = getResources().getIdentifier(icon, "drawable", getPackageName());
        				if (id == 0)
        					throw new Resources.NotFoundException();
            		} catch (Resources.NotFoundException e) {
            			id = DEFAULT_ICON;
            		}
            	}
            	
                sendNotification(header, bar, message, id);
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
	}
	
    private void sendNotification(String header, String bar, String message, int id) {
        mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        
        Intent intent = new Intent(this, ShopActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        
        NotificationCompat.Builder mBuilder =
        		new NotificationCompat.Builder(this)
        			.setAutoCancel(true)
        			.setSmallIcon(id)
        			.setTicker(bar)
			        .setContentTitle(header)
			        .setContentText(message)
			        .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
			        .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
        
        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    }
}